var hour = document.querySelector('.hour');
var minute = document.querySelector('.minute');
var second = document.querySelector('.second');


//倒计时
var inputTime = +new Date('2021-7-23 24:00:00'); //倒计时的结束时间，自己设置时间
countDown(); //先调用一次这个函数 防止第一次刷新页面有空白
//2、开启定时器
setInterval(countDown, 1000); //1000毫秒，每一秒钟调用一次函数
//3、倒计时-时分秒函数
function countDown() {
  var nowTime = +new Date(); //返回的是当前时间的总的毫秒数
  var times = (inputTime - nowTime) / 1000; // times是剩余时间的总的毫秒数
  var h = parseInt(times / 60 / 60 % 24);
  h = h < 10 ? '0' + h : h; //判断数值小于10的情况 如 0-9改为 00-09
  hour.innerHTML = h; //更改div里面的内容 把h给获取元素hour的内容
  var m = parseInt(times / 60 % 60);
  m = m < 10 ? '0' + m : m;
  minute.innerHTML = m; //同上
  var s = parseInt(times % 60);
  s = s < 10 ? '0' + s : s;
  second.innerHTML = s; //同上
}

// 大轮播
var banner1 = new Swiper('.banner1', {
  loop: true, 
  pagination: {
    el: '.swiper-pagination',
  },
  navigation: {
    nextEl: '.swiper-button-next',
    prevEl: '.swiper-button-prev',
  },
  autoplay: {
    delay: 1000, //1秒切换一次
  },
})


// <!----------------三位一体轮播 ----------------------->
var banner2 = new Swiper('.banner2', {

  loop: true, // 循环模式选项

  navigation: {
    nextEl: '.swiper-button-next',
    prevEl: '.swiper-button-prev',
  },
  autoplay: {
    delay: 5000, //1秒切换一次
  },
})


let obj = {
  index: 1,
  len: 10
}
let commodity = document.querySelector('.commodity')
let flag = true;

// 下拉加载
window.onscroll = function () {
  if (scrollY >= 1000 + commodity.offsetHeight - innerHeight && flag == true) {
    flag = false
    pAjax({
      url: "./api/getData.php",
      data: {
        index: obj.index++,
        len: obj.len
      }
    }).then(res => {

      res = JSON.parse(res)
      // console.log(res.list)
      render(res.list)
      flag = true;
    });
  }
}
let str = '';

function render(data) {
  data.forEach((item, index) => {
    // console.log(item);
    str += `  <div>
    <img src="${item.goods_big_logo}" alt="">
    <div>
        <a href="./html/detail.html?id=${item.goods_id}">${item.goods_name}</a>
        <span>￥${item.goods_price}<span>
    </div>
      </div>`
  });
  commodity.innerHTML = str;
}
let denglv = document.querySelector('.denglv');
let login = getCookie("login")

if (login) {
  denglv.innerHTML = `欢迎你，${login}`
}


 

let seckillbanner = document.querySelector(".banner3")
let sw = seckillbanner.querySelector(".swiper-wrapper")


imgdata()

async function imgdata() {
  let res = await pAjax({
    url: './json/banner.json'
  })
  res = JSON.parse(res);
  render3(res.banner3);

  let banner3 = new Swiper('.banner3', {
    slidesPerView: 4,
    slidesPerGroup: 4,
    loop: true,
    loopFillGroupWithBlank: true,
    navigation: {
      nextEl: '.swiper-button-next',
      prevEl: '.swiper-button-prev',
    },

  })
}

function render3(data) {
  let str = '';
  data.forEach(item => {
    str += ` <div class="content">
    <img src="./images/aa.jpg" alt="">
    <h6>"【520告白季礼物】红谷包包女包新款潮流行百搭腋下包女牛皮复古洋气单肩新款手提包 米白"</h6>
    <div>
        <span style="background-color: red;">￥61516</span>
        <span>￥13515</span>
    </div>
</div>`
  })
  sw.innerHTML = str;

}